/**
 * created by 小卷毛, 2020-05-11 011
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.view

import android.animation.*
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Point
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.appcompat.widget.AppCompatTextView
import com.aifeng.common_ui.base.getColorEx
import com.aifeng.common_ui.helper.dp2px
import com.aifeng.hassan_food.R

/**
 * 购物车添加动画：贝塞尔曲线和缩放
 */
class BeizerTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    companion object {
        const val VIEW_SIZE = 20
        const val VIEW_END_SIZE = 14
    }

    private var mPaint4Circle: Paint = Paint()
    private var radius = 0
    private var startPosition: Point? = null
    private var endPosition: Point? = null

    init {
        mPaint4Circle.color = context.getColorEx(R.color.colorAccent)
        mPaint4Circle.isAntiAlias = true
        gravity = Gravity.CENTER
        text = "1"
        setTextColor(context.getColorEx(R.color.white))
        textSize = 12f
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(
            dp2px(VIEW_SIZE.toFloat()),
            dp2px(VIEW_SIZE.toFloat())
        )
        radius = dp2px(VIEW_SIZE.toFloat()) / 2
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawCircle(
            measuredWidth / 2.toFloat(),
            measuredHeight / 2.toFloat(),
            radius.toFloat(),
            mPaint4Circle
        )
        super.onDraw(canvas)
    }

    fun setStartPosition(startPosition: Point) {
        startPosition.y -= VIEW_SIZE / 2
        this.startPosition = startPosition
    }

    fun setEndPosition(position: Point) {
        position.x -= VIEW_END_SIZE / 2
        position.y -= VIEW_END_SIZE / 2
        this.endPosition = position
    }

    fun startBeizerAnimation(onFinished: () -> Unit = {}) {
        if (startPosition == null || endPosition == null) return

        val bezierEvaluator = BezierEvaluator(
            Point(
                (startPosition!!.x + endPosition!!.x) / 2,
                startPosition!!.y - dp2px(100f)
            )
        )

        val animBeizer = ValueAnimator.ofObject(
            bezierEvaluator,
            startPosition,
            endPosition
        )

        animBeizer.addUpdateListener {
            val point = it.animatedValue as Point
            x = point.x.toFloat()
            y = point.y.toFloat()
            invalidate()
        }

        val animSize = ValueAnimator.ofFloat(1.0f, 0.7f)

        animSize.addUpdateListener {
            scaleX = it.animatedValue as Float
            scaleY = it.animatedValue as Float
        }

        AnimatorSet().apply {
            playTogether(animBeizer, animSize)
            duration = 300
            interpolator = AccelerateDecelerateInterpolator()
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    val viewGroup = parent as ViewGroup
                    viewGroup.removeView(this@BeizerTextView)
                    onFinished.invoke()
                }
            })
            start()
        }
    }

    inner class BezierEvaluator(private val controllPoint: Point) :
        TypeEvaluator<Point> {
        override fun evaluate(
            fraction: Float,
            startValue: Point,
            endValue: Point
        ) = Point(
            ((1 - fraction) * (1 - fraction) * startValue.x + 2 * fraction * (1 - fraction) * controllPoint.x + fraction * fraction * endValue.x).toInt(),
            ((1 - fraction) * (1 - fraction) * startValue.y + 2 * fraction * (1 - fraction) * controllPoint.y + fraction * fraction * endValue.y).toInt()
        )
    }

}